/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean.chart;

import com.bean.chart.editors.ColorPanel;
import com.bean.chart.iterfaces.AbstractChartLegend;
import com.bean.chart.iterfaces.DataObserver;
import com.bean.chart.iterfaces.LegendDataProvider;
import java.awt.*;
import java.beans.*;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author sammael
 */
public class ChartLegend extends JPanel implements AbstractChartLegend, DataObserver, Serializable {
    
    public final static String PROP_LABEL = "label";
    public final static String PROP_BACKGROUND = "back";
    public final static String PROP_FOREGROUND = "fore";
    public final static String PROP_LABEL_VISIBLE = "labelVisible";
    public final static String PROP_SAMPLE_VISIBLE = "sampleVisible";
    public final static String PROP_ROWCOUNT = "rowcount";
    public final static String PROP_COLUMNCOUNT = "coulmncount";
    public final static String PROP_SAMPLE_SIZE = "samplesize";
    
    //Properties
    private PropertyChangeSupport propertySupport;
    private String label = "Przykładowa legenda";
    private Color legendBackground = Color.LIGHT_GRAY;
    private Color legendForeground = Color.DARK_GRAY;
    private boolean legendLabelVisible = true;
    private boolean colorSampleVisible = true;
    private int rowCount = 3;
    private int columnCount = 2;
    private Dimension sampleSize = new Dimension(40, 20);
    private LegendDataProvider provider;
    
    //Components
    private ChartLabel labelLbl = new ChartLabel();
    //private ArrayList<JLabel> chartName;
    //private ArrayList<ColorPanel> chartColor;
    private ArrayList<JPanel> panels = new ArrayList<>();
    
    private JPanel grid = new JPanel();
    //private ChartLabel chartLabel;
    
    public ChartLegend() {
        propertySupport = new PropertyChangeSupport(this);
        this.setLayout(new BorderLayout());
        labelLbl.setTitleText(label);
        labelLbl.setTitleBackground(legendBackground);
        redistribution();
    }

    @Override
    public void setLegendLabel(String label) {
        String old = this.label;
        this.label = label;
        labelLbl.setTitleText(label);
        propertySupport.firePropertyChange(PROP_LABEL, old, label);
        
    }

    @Override
    public String getLegendLabel() {
        return label;
    }

    @Override
    public void setLegendBackground(Color color) {
        Color old = this.legendBackground;
        this.legendBackground = color;
        labelLbl.setTitleBackground(legendBackground);
        
        propertySupport.firePropertyChange(PROP_BACKGROUND, old, color);
        redistribution();
    }

    @Override
    public Color getLegendBackground() {
        return legendBackground;
    }

    @Override
    public void setLegendForeground(Color color) {
        Color old = this.legendForeground;
        this.legendForeground = color;
        propertySupport.firePropertyChange(PROP_FOREGROUND, old, color);
        redistribution();
    }

    @Override
    public Color getLegendForeground() {
        return legendForeground;
    }

    @Override
    public void setLegendLabelVisible(boolean visible) {
        boolean old = this.legendLabelVisible;
        this.legendLabelVisible = visible;
        propertySupport.firePropertyChange(PROP_LABEL_VISIBLE, old, visible);
        redistribution();
    }

    @Override
    public boolean isLegendLabelVisible() {
        return this.legendLabelVisible;
    }

    @Override
    public void setColorSampleVisible(boolean visible) {
        boolean old = this.colorSampleVisible;
        colorSampleVisible = visible;
        propertySupport.firePropertyChange(PROP_SAMPLE_VISIBLE, old, visible);
        redistribution();
    }

    @Override
    public boolean isColorSampleVisible() {
        return colorSampleVisible;
    }

    @Override
    public void setRowCount(int count) {
        int old = rowCount;
        rowCount = count;
        propertySupport.firePropertyChange(PROP_ROWCOUNT, old, count);
        redistribution();
    }

    @Override
    public int getRowCount() {
        return rowCount;
    }

    @Override
    public void setColumnCount(int count) {
        int old = columnCount;
        columnCount = count;
        propertySupport.firePropertyChange(PROP_COLUMNCOUNT, old, count);
        redistribution();
    }

    @Override
    public int getColumnCount() {
        return columnCount;
    }

    @Override
    public void setColorSampleSize(Dimension d) {
        Dimension old = sampleSize;
        sampleSize = d;
        propertySupport.firePropertyChange(PROP_SAMPLE_SIZE, old, d);
        redistribution();
    }

    @Override
    public Dimension getColorSampleSize() {
        return sampleSize;
    }

    @Override
    public void setDataProvider(LegendDataProvider provider) {
        this.provider = provider;
        redistribution();
    }

    @Override
    public LegendDataProvider getDataProvider() {
        return provider;
    }

    @Override
    protected void paintComponent(Graphics g) {
        
        g.setColor(legendBackground);
        g.fillRect(0, 0, getSize().width, getSize().height);
        grid.setBackground(legendBackground);
        for(JPanel p: panels)
            p.setBackground(legendBackground);
        super.paintComponent(g);
    }
    
    private void redistribution(){
        removeAll();
        //chartColor.clear();
        //chartName.clear();
        //panels.clear();
        if(isLegendLabelVisible()){
            
            this.add(BorderLayout.NORTH, labelLbl);
        }
        try{
        grid.removeAll();
        grid.setLayout(new GridLayout(rowCount, columnCount, 5, 7));
        HashMap<String,Color> charts = provider.getData();
        Set entries = charts.entrySet();
        Iterator i = entries.iterator();
        panels.clear();
        while(i.hasNext()){
            Map.Entry<String,Color> entry = (Map.Entry)i.next();
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 7));
            JLabel lbl = new JLabel(entry.getKey());
            ColorPanel sample;
            
            if(isColorSampleVisible()){
                sample = new ColorPanel(entry.getValue());
                sample.setPreferredSize(sampleSize);
                sample.setMinimumSize(sampleSize);
                panel.add(sample);
            
            }else{
                lbl = new JLabel(entry.getKey());
                lbl.setForeground(entry.getValue());
            
            }
            panel.add(lbl);
            panels.add(panel);
            grid.add(panel);
            repaint();
        }
        this.add(grid, BorderLayout.CENTER);
        
        }catch(NullPointerException npe){
            provider = new LegendDataProvider() {

                @Override
                public HashMap<String, Color> getData() {
                    HashMap<String,Color> data = new HashMap<>();
                    data.put("Wykres 1", Color.BLUE);
                    data.put("Wykres 2", Color.RED);
                    data.put("Wykres 3",Color.ORANGE);
                    data.put("Wykres 4", Color.GREEN);
                    return  data;
                }
            };
            redistribution();
        }
    }

    @Override
    public void update() {
        redistribution();
    }
    
    
}
